<?php

namespace App\Http\Admin\Services\Mate;

use App\Exceptions\InvalidRequestException;
use App\Http\Models\Mate;
use App\Http\Models\MateItem;
use App\Http\Services\BaseService;


class MateService extends BaseService
{
    public function list($conditions = []){
        $query = Mate::query();

        //关键词
        if(!empty($conditions['keywords'])){
            $str = $conditions['keywords'];

            if(strpos($str,',')){
                $keyword_arr = explode(',',$str);
                if(count($keyword_arr) > 0){
                    foreach ($keyword_arr as $keyword){
                        $query = $query->where('tags','like',"%$keyword%");
                    }
                }
            }else{
                $query->where('tags','like',"%$str%")->orWhere('title','like',"%$str%");
            }
        }

        if(!empty($conditions['content_id'])){
            $content_id = $conditions['content_id'];
            $query->where('content_id',$content_id);
        }

        $result = $query->inRandomOrder()->with('items')->paginate(4);

        return $result;
    }

    public function getWords($conditions = []){
        $query = MateItem::query();
        //关键词
        $result = $query->whereHas('mate',function ($query) use($conditions){
            $query->where('platform',0);
            if(!empty($conditions['word'])){
                $str = $conditions['word'];
                $query->where('content','like',"%$str%");
            }
        })->where('type',1)->inRandomOrder()->take(30)->get()->pluck('content');
        return $result;
    }



}
